<?php

class DocsController extends BaseController
{
    protected $project;

    public function preAction()
    {
        parent::preAction();

        $this->view->assign('tree', PhpdocPackage::getPackageTree());

        $name = $this->getParam('project');
        $this->project = Project::getByParams(array('name' => $name));
        if (!$this->project) {
            Url::redirect('/projects/');
        }

        $this->view->assign('project', $this->project);
    }

    public function defaultAction()
    {
        $this->view->assign('nav', array('Проект ' . $this->project->title => '/projects/view/' . $this->project->name,
                                         'Документація' => '/project/' . $this->project->name . '/docs/'));

        $this->view->display('docs.default.tpl');
    }

    public function pathViewAction()
    {
        $path = trim($this->getParam('path'),'/');
        
        $file = end(explode('/',$path));        
        $path = substr($path, 0, -strlen($file) - 1);
        
        $folder = PhpdocFolder::getFolder($path);
        if (!$folder) {
            Url::redirect('/projects/');
        }
        $file = PhpdocFile::getByParams(array('folder_id' => $folder->id, 'name' => $file));		

        /*$cov = $file->Coverage->get();
        $coverage = array();
        foreach ($cov as $itm) {
            $coverage[$itm->line] = array('status' => $itm->status,
                                          'tests' => $itm->test_count);
        }
        $this->view->assign('coverage', $coverage);

        
*/
        $this->view->assign('coverage', array());
		$sourceFile = $this->project->Settings->highlighted_source_path . $file->source;
		if(file_exists($sourceFile)) {
			$this->view->assign('lines', explode("\n", ltrim(file_get_contents($sourceFile))) );
		}
        $this->view->assign('package', $package);
        $this->view->assign('nav', array('Проект ' . $this->project->title => '/projects/view/' . $this->project->name,
                                         'Документація' => '/project/' . $this->project->name . '/docs/',
                                         'Файл ' . $file->name => '#'));

        //$this->view->assign('comments', $file->Comment->get());
        $this->view->assign('file', $file);
        $this->view->display('file.id.tpl');
    }

    public function packageViewAction()
    {
        $packageName = $this->getParam('package');        
        $package = PhpdocPackage::getByParams(array('name' => $packageName));
        if (!$package) {
            Url::redirect('/project/' . $this->project->name . '/docs/');
        }

        $this->view->assign('package', $package);
		$this->view->assign('classInfo', $this->getClasses($package));
        $this->view->assign('nav', array('Проект ' . $this->project->title => '/projects/view/' . $this->project->name,
                                         'Документація' => '/project/' . $this->project->name . '/docs/',
                                         'Пакет ' . $package->name => '#'));

        $this->view->display('docs.package.tpl');
    }

    public function showclassAction()
    {
        $className = $this->getParam('className');		
        $class = PhpdocClass::getByParams(array('name' => $className));

        if (!$class) {
            Url::redirect('/project/' . $this->project->name . '/docs/');
        }

        $this->view->assign('class', $class);
        $package = $class->PhpDocPackage;
        $this->view->assign('package', $package);
        $this->view->assign('classInfo', $this->getClasses($package));
        $this->view->assign('classTree', $class->getClassTree());
		$this->view->assign('nav', array('Проект ' . $this->project->title => '/projects/view/' . $this->project->name,
                                         'Документація' => '/project/' . $this->project->name . '/docs/',
                                         'Пакет '. $package->name  => '/project/bazalt/docs/package/' . $package->name .'/',
                                         'Клас ' . $class->name => '#'));		
 /*       
        $covInfo = CoverageLineStatus::getFileCoverageInfo($class->Pfile->id);
        $sum = 0;
        foreach($covInfo as &$cov) {
            $sum += $cov->count;
        }
        foreach($covInfo as &$cov) {
            switch($cov->status) {
                case 0:
                case 1:
                    $sum -= $cov->count;
                break;
                case 3:
                    $covered = $cov->count;
                break;                 
            }
        }
        $covered = ($sum > 0) ? round(($covered / $sum) * 100, 2) : 0;
        $this->view->assign('covered', $covered);
        
        $projSet = $this->project->Settings;
        if($covered <= $projSet->low_cover_level) {
            $this->view->assign('covLevelClass', 'bad-cover' );
        } elseif($covered > $projSet->low_cover_level && $covered <= $projSet->middle_cover_level) {
            $this->view->assign('covLevelClass', '' );
        } else {
            $this->view->assign('covLevelClass', 'good-cover' );
        }
*/
		/*$covInfo = CoverageLineStatus::getFunctionsCoverafeInfo($class->Pfile->id,$class->id);
		$sum = count($covInfo);
		$covered = 0;
        foreach($covInfo as &$cov) {            
			if($cov->covered == 1) {
				$covered++;
			}
        }
		$this->view->assign('methodsCoveredCount', $covered );
		$this->view->assign('methodsCount', $sum );
		$methodsCovered = ($sum > 0) ? round(($covered / $sum) * 100, 2) : 0;
		$this->view->assign('methodsCovered', $methodsCovered);
		$this->view->assign('methodsCoveredpx', round( 120 - ($methodsCovered * 120 / 100) ) );
		*/
        
        $this->view->assign('class_location', $class->PhpdocFile->getLocation());
        $this->view->assign('class_tags', $class->getTags());
        
        $class_methods = $class->PhpDocMethods->get();
        $class_inherited_methods = $class->getPhpDocInheritedMethods();
        $class_variables = $class->PhpdocVariables->get();
        $class_inherited_variables = $class->getPhpDocInheritedVariables();
        $class_consts = $class->PhpdocConsts->get();
        $class_inherited_consts = $class->getPhpDocInheritedConsts();
        
        $this->view->assign('class_methods', $class_methods);
        $this->view->assign('class_inherited_methods', $class_inherited_methods);
        $this->view->assign('class_variables', $class_variables);
        $this->view->assign('class_inherited_variables', $class_inherited_variables);
        $this->view->assign('class_consts', $class_consts);
        $this->view->assign('class_inherited_consts', $class_inherited_consts);
        
        $this->view->assign('count_methods', count($class_methods));
        $this->view->assign('count_variables', count($class_variables));
        $this->view->assign('count_consts', count($class_consts));
        
        $this->view->display('docs.class.tpl');
    }
	
	public function getClasses($package)
    {
		$res = array();
		$res['classes'] = array();
		$res['subpackages'] = array();
		$res['interfaces'] = array();
		
		$subpackages = $package->getSubpackages();        
		foreach($subpackages as &$subpackage) {            
			foreach($subpackage->Classes->get() as $class) {
				if($class->is_interface) {
					$res['interfaces'][$class->name] = $class;
				} else {
					if($subpackage->subpackage == '') {
						$res['classes'][$class->name] = $class;
					} else {
						if(!array_key_exists($subpackage->subpackage,$res['subpackages'])) {
							$res['subpackages'][$subpackage->subpackage] = array();
						}
						$res['subpackages'][$subpackage->subpackage][$class->name] = $class;                        
					}
				}
			}
		}
		return $res;
    }

    public function errorsAction()
    {
        $this->view->display('docs.errors.tpl');
    }

    public function todoAction()
    {
        $this->view->assign('todo_list', PhpdocTag::getByParams(array('name'=>'todo'),ORMRecord::MANY_RECORDS));
        $this->view->display('docs.todo.tpl');
    }
}
